﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace FriendlyFire.UserControls
{
    public partial class Friends : Form
    {
        Helper myHelper;
        System.Net.IPEndPoint addingThisGuy = null;
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="myHelper">Reference to friendly fire helper</param>
        public Friends(ref Helper h)
        {
            InitializeComponent();
            myHelper = h;
        }

        /// <summary>
        /// Event on form load, gets information from the friends table of the database
        /// </summary>
        /// <param name="sender">Object sending the event</param>
        /// <param name="e">any event arguments that need to be passed</param>
        private void Friends_Load(object sender, EventArgs e)
        {
            RefreshFriendsList();
            foreach (Control c in this.Controls)
            {
                c.KeyUp += new KeyEventHandler(Friends_KeyUp);
            }

        }

        private delegate void RefreshFriendsListDelegate();
        public void RefreshFriendsList()
        {
            if (listFriends.InvokeRequired)
            {
                listFriends.Invoke(new RefreshFriendsListDelegate(RefreshFriendsList));
            }
            else
            {
                listFriends.DataSource = myHelper.DBHelper.GetFriends().ToList();
                listFriends.ValueMember = "Key";
                listFriends.DisplayMember = "Value";
            }
        }

        #region AddFriend
        /// <summary>
        /// Lookups up the host and port.
        /// Negotiates public key transfer.
        /// </summary>
        /// <param name="sender">Object sending the event</param>
        /// <param name="e">any event arguments that need to be passed</param>
        private void btnAddFriend_Click(object sender, EventArgs e)
        {  
            try
            {
                //System.Net.IPEndPoint hostIP = new IPEndPoint(Dns.GetHostAddresses(txtHostName.Text)[0],);
                //myParent.SendAddFriendRequest(txtHostName.Text,null,true);
                //addingThisGuy = hostIP;
                fTimer.Enabled = true;
            }
            catch (Exception err)
            {
                MessageBox.Show("Invalid Hostname or Port");
            }
        }
        #endregion

        /// <summary>
        /// Removes a friend from the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemoveFriend_Click(object sender, EventArgs e)
        {
            if (listFriends.SelectedIndex != -1)
            {
                try
                {
                    //myParent.ffHelper.DeleteFriendByName(Convert.ToInt32(listFriends.SelectedValue));
                    this.RefreshFriendsList();
                }
                catch
                {
                    MessageBox.Show("Invalid friend");
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void Friends_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            //if (e.KeyCode == Keys.Enter)  // no specal ok code needed. just using the OK property of the form.
            //{
            //    this.btnOK_Click(null, null);
            //}
        }

        private void Friends_Paint(object sender, PaintEventArgs e)
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            myHelper.DrawDefaultBackGroundGradient(e.Graphics, ClientRectangle);
        }

        private void fTimer_Tick(object sender, EventArgs e)
        {
            if (addingThisGuy != null)
            {
                //myParent.ClientsOnHold.Remove(addingThisGuy);
                MessageBox.Show("Your friend request has timed out while waiting for approval.", "Time Out", MessageBoxButtons.OK);
                addingThisGuy = null;
            }
            fTimer.Enabled = false;
        }
    }
}
